// app.js
App({
  globalData: {
    root_url: 'http://127.0.0.1:8000/page/',
    user_id: '',
    login: {
      show: false,
      avatar: 'https://img0.baidu.com/it/u=3204281136,1911957924&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'
    },
    basic_info: {
      name: '',
      academy: '',
      licence_number: '',
      contact_infomation: ''
    }
  }, 

  onLaunch(event) {
    wx.login({
      success: (res) => {
        const code = res.code
        wx.request({
          url: this.globalData.root_url + 'get_openid/',
          method: 'POST',
          data: JSON.stringify({'code': code}),
          success: (res) => {
            this.globalData.user_id = res.data
            wx.request({
              url: this.globalData.root_url + 'get_basic_info/?user_id=' + this.globalData.user_id,
              method: 'GET',
              success: (res) => {
                if (Object.keys(res.data).length != 0) {
                  this.globalData.login.show = true
                  if (res.data['avatar'] != '') {
                    this.globalData.login.avatar = res.data['avatar']
                  }
                  this.globalData.basic_info = {
                    name: res.data['name'],
                    academy: res.data['academy'],
                    licence_number: res.data['licence_number'],
                    contact_infomation: res.data['contact_infomation']
                  }
                }
              }
            })
          }
        })
      },
    })
    
  }
})
